using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FireStarterModeller.BO;

namespace FireStarter.CodeGeneration
{

    public class SolutionFileManager
    {
        private readonly string _solutionFileName;
        private readonly CodeTemplateEnum _codeTemplate;
        private string _contents;

        public SolutionFileManager(string solutionFileName, CodeTemplateEnum codeTemplate)
        {
            _solutionFileName = solutionFileName;
            _codeTemplate = codeTemplate;
            if (File.Exists(solutionFileName))
            {
                _contents = File.ReadAllText(solutionFileName);
            } else
            {
                _contents = TemplateLoader.Sln(codeTemplate);
            }
        }

        public string SolutionFileName
        {
            get { return _solutionFileName; }
        }

        public void SaveSolutionFile()
        {
            Utilities.CreateFile(_solutionFileName, _contents, true);
        }

        public bool HasProjectFile(string fileName)
        {
            string projectDefs;
            string projectConfigurations;
            GetProjectContentLines(fileName, out projectDefs, out projectConfigurations);
            if (_contents.IndexOf(projectDefs, StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                if (_contents.IndexOf(projectConfigurations, StringComparison.InvariantCultureIgnoreCase) != -1)
                {
                    return true;
                }
            }
            return false;
        }

        public void AddProjectFile(string fileName)
        {
            if (HasProjectFile(fileName)) return;
            string projectDefs;
            string projectConfigurations;
            GetProjectContentLines(fileName, out projectDefs, out projectConfigurations);
            string markerTag = (_codeTemplate == CodeTemplateEnum.CSharpVisualStudio2008)
                                   ? "# Visual Studio 2008"
                                   : "# Visual Studio 2005";
            AddStringAfterTag(markerTag, projectDefs);
            AddStringAfterTag("GlobalSection(ProjectConfigurationPlatforms) = postSolution", projectConfigurations);
        }

        private void AddStringAfterTag(string tagName, string stringToInsert)
        {
            int pos = _contents.IndexOf(tagName);
            _contents = _contents.Insert(pos + tagName.Length, stringToInsert);
        }

        private void GetProjectContentLines(string fileName, out string projectDefs, out string projectConfigurations)
        {
            ProjectFileManager projectFileManager = new ProjectFileManager(fileName, OutputType.Library, _codeTemplate);
            string projectGuidString = projectFileManager.ProjectGuid.ToString("B").ToUpper();

            string relativePath = fileName;
            if (Path.IsPathRooted(fileName))
            {
                relativePath = Utilities.GetRelativePath(relativePath, Path.GetDirectoryName(_solutionFileName));
            }
            projectDefs = string.Format(
                "\r\nProject(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{0}\", \"{1}\", \"{2}\"\r\nEndProject", 
                projectFileManager.AssemblyName, relativePath, projectGuidString);

            projectConfigurations =  string.Format(
                "\r\n\t\t{0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU" + 
                "\r\n\t\t{0}.Debug|Any CPU.Build.0 = Debug|Any CPU" + 
                "\r\n\t\t{0}.Release|Any CPU.ActiveCfg = Release|Any CPU" + 
                "\r\n\t\t{0}.Release|Any CPU.Build.0 = Release|Any CPU", 
                projectGuidString);
        }
    }
}
